package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.ogilvy.cms.bean.CmsPage;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.frame.action.BaseAction;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsPageCommentAction")
public class CmsPageCommentAction extends BaseAction {

	@Autowired
	protected ServiceDispatcher serviceDispatcher;
	private String rows;// 前台以JSON的形式传递数据
	List<CmsPage> cmsPagelist;

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	/**
	 * 根据父页Id以及模板Id获取CMS_Page信息
	 */
	public void queryCmsPageBytIdpId() {
		Map<String, String> screenMap = new HashMap<String, String>();
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		String parentCmpId = js.getString("parentCmpId")!=null?js.getString("parentCmpId"):"2";
		screenMap.put("cmpCmtId", 1 + "");
		screenMap.put("parentCmpId", parentCmpId);
		cmsPagelist = serviceDispatcher.getCmsPageCommentService()
				.selectByCmtIdParentId(screenMap);
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		Iterator<CmsPage> it = cmsPagelist.iterator();
		while (it.hasNext()) {
			CmsPage cp = it.next();
			sb.append("{parent:"+cp.getParentCmpId()+",");
			sb.append("cmpId:"+cp.getCmpId()+",");
			sb.append("text:'"+cp.getCmpName()+"',");
			boolean bool = cp.getCmpIsColumn() == 0 ? false : true;
			sb.append("leaf:"+bool);
			sb.append("},");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(sb.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据父页Id以及模板Id获取子节点的信息
	 */
	public void queryChild() {
		Map<String, String> screenMap = new HashMap<String, String>();
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		String parentCmpId = js.getString("parentCmpId")!=null?js.getString("parentCmpId"):"-1";
		screenMap.put("cmpCmtId", 1 + "");
		screenMap.put("parentCmpId", parentCmpId);
		cmsPagelist = serviceDispatcher.getCmsPageCommentService()
				.selectByCmtIdParentId(screenMap);
		StringBuffer sb = new StringBuffer();
		if(cmsPagelist.size() != 0){
			sb.append("{success:true,rows:");
			sb.append("[");
			Iterator<CmsPage> it = cmsPagelist.iterator();
			while (it.hasNext()) {
				CmsPage cp = it.next();
				sb.append("{parent:"+cp.getParentCmpId()+",");
				sb.append("cmpId:"+cp.getCmpId()+",");
				sb.append("text:'"+cp.getCmpName()+"',");
				boolean bool = cp.getCmpIsColumn() == 0 ? false : true;
				sb.append("leaf:"+bool+",");
				sb.append("children:[]");
				sb.append("},");
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append("]}");
		} else {
			sb.append("{success:false}");
		}
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
System.out.println("========================================");
System.out.println(sb.toString());
System.out.println("========================================");
			out.print(sb.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
